﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;

namespace GIAODIEN
{
    public partial class frmthemnhanvien : Form
    {
        public frmthemnhanvien()
        {
            InitializeComponent();
        }
        void hienthilistview()
        {
            lvhienthi.Items.Clear();
            DataTable dsnv = new DataTable();
            dsnv = BUS_nhanvien.LayDSNhanVien();
            ListViewItem it = new ListViewItem();
            foreach (DataRow dr in dsnv.Rows)
            {
                it = lvhienthi.Items.Add(dr["manv"].ToString());
                for (int i = 1; i < dsnv.Columns.Count; i++)
                    if (i == 2)
                        it.SubItems.Add(DateTime.Parse(dr[i].ToString()).ToShortDateString());
                    else
                        it.SubItems.Add(dr[i].ToString());


            }
        }
        void loadcbo()
        {
            DataTable dstg = new DataTable();
            dstg = BUS_LoaiNV.layloainv();
            cboloainv.DataSource = dstg;
            cboloainv.DisplayMember = "tenloainv";
            cboloainv.ValueMember = "maloainv";
            cboloainv.SelectedIndex = -1;
            cboloainv.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            cboloainv.AutoCompleteSource = AutoCompleteSource.ListItems;
        }
        private void frmthemnhanvien_Load(object sender, EventArgs e)
        {
            bientoancuc.nv = 1;
            hienthilistview();
            loadcbo();
        }

        private void btnthem_Click(object sender, EventArgs e)
        {
            DTO_NhanVien n = new DTO_NhanVien();
            n.TenNhanVien = txttennv.Text;
            n.TenDN = txttendn.Text;
            n.SDT_NV = txtsdt.Text;
            n.DiaChi = rtxtdc.Text;
            n.MaLoaiNV = cboloainv.SelectedValue.ToString();
            n.MatKhau = txtmk.Text;
            n.NgaySinh = dtpkns.Text;
            if (BUS_nhanvien.themnhanvien(n))
            {
                MessageBox.Show("Thêm thành công"," Thông báo");
                hienthilistview();
            }
        }

        private void lvhienthi_MouseDown(object sender, MouseEventArgs e)
        {
            ListViewItem it = new ListViewItem();
            it = lvhienthi.GetItemAt(e.X, e.Y);
            if (it != null)
            {
                txtmanv.Text = it.SubItems[0].Text;
                txttennv.Text = it.SubItems[1].Text;
                dtpkns.Text = it.SubItems[2].Text;
                rtxtdc.Text = it.SubItems[3].Text;
                txtsdt.Text = it.SubItems[4].Text;
                cboloainv.SelectedIndex = int.Parse(it.SubItems[5].Text);
                txttendn.Text = it.SubItems[6].Text;
                txtmk.Text = it.SubItems[1].Text;
                
            }
            anhienbtn(true);
        }
        void anhienbtn(Boolean b)
        {
            btnxoa.Enabled = b;
            btnsua.Enabled = b;
        }
        private void btnxoa_Click(object sender, EventArgs e)
        {
            DTO_NhanVien n = new DTO_NhanVien();
            n.MaNhanVien = txtmanv.Text;
            DialogResult tb = new DialogResult();
            tb = MessageBox.Show("Bạn có muốn xóa", "thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (tb == DialogResult.Yes)
                if (BUS_nhanvien.xoanhanvien(n))
                {
                    MessageBox.Show("Xóa thành công", "thông báo");
                    hienthilistview();
                    anhienbtn(false);
                  

                }
        }

        private void btnsua_Click(object sender, EventArgs e)
        {
            DTO_NhanVien n = new DTO_NhanVien();
            n.MaNhanVien = txtmanv.Text;
            n.TenNhanVien = txttennv.Text;
            n.TenDN = txttendn.Text;
            n.SDT_NV = txtsdt.Text;
            n.DiaChi = rtxtdc.Text;
            n.MaLoaiNV = cboloainv.SelectedValue.ToString();
            n.MatKhau = txtmk.Text;
            n.NgaySinh = dtpkns.Text;
            if (BUS_nhanvien.suanhanvien(n))
            {
                MessageBox.Show("Sửa thành công", " Thông báo");
                hienthilistview();
                anhienbtn(false);
            }

        }
    }
}
